#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace KLib
{
    public class Pool<T> where T : new()
    {
        #region Fields
        private Stack<T> stack;
        #endregion

        #region Initialization

        public Pool()
        {
            stack = new Stack<T>();
        }

        public Pool(int size)
        {
            stack = new Stack<T>(size);
        }

        #endregion

        #region Methods
        /// <summary>
        /// Gets an item from the pool.
        /// </summary>
        /// <returns></returns>
        public T Fetch()
        {
            if (stack.Count > 0)
                return stack.Pop();
            else
                return new T();
        }

        /// <summary>
        /// Free an item from the pool.
        /// </summary>
        /// <param name="item"></param>
        public void Release(T item)
        {
            stack.Push(item);
        }
        #endregion
    }
}
